package pub.tbc.toolkit.encode;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pub.tbc.toolkit.Loops;
import pub.tbc.toolkit.Objs;
import pub.tbc.toolkit.function.Consumer;

import java.util.Random;
import java.util.UUID;

/**
 * @author tbc
 * @version 1.0 {2016年7月8日 下午4:45:56}
 */
public class CodeHelper {
    private Logger log = LoggerFactory.getLogger(CodeHelper.class);

    /**
     * @return
     */
    public String getDefaultPassword() {
        return MD5.getMD5("123456");
    }

    /**
     * 生成8位默认密码
     *
     * @return .
     */
    public String defaultPass() {
        return randomPass(8);
    }

    /**
     * 生成指定长度的随机密码
     *
     * @param l:密码长度，不要太长
     * @return .
     */
    public String randomPass(int l) {
        StringBuilder sb = new StringBuilder();
        sb.append("a,b,c,d,e,f,g,h,i,g,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z");
        sb.append(",A,B,C,D,E,F,G,H,I,G,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z");
        // sb.append(",~,@,#,$,%,^,&,*,(,),_,+,|,`,.");//15
        // sb.append(",@,#,$,%,&,_");//6
        sb.append(",1,2,3,4,5,6,7,8,9,0");
        sb.append(",_");

        String[] arr = sb.toString().split(",");
        // List pswdList = new ArrayList();
        StringBuilder b = new StringBuilder();
        for (int i = 0; i < l; i++) {
            // 回一个伪随机数，它是取自此随机数生成器序列的、在 0（包括）和指定值（((不包括))）之间均匀分布的 int 值。
            b.append(String.valueOf(arr[new Random().nextInt(arr.length)]));
        }
        return b.toString();
    }

    /**
     * 加密，使用Base64加密算法加密字符串
     *
     * @param plainText .
     * @return .
     */
    public static String base64Encode(String plainText) {
        byte[] b = plainText.getBytes();
        Base64 base64 = new Base64();
        b = base64.encode(b);
        return new String(b);
    }

    /**
     * 解密
     *
     * @param encodeStr .
     * @return .
     */
    public static String base64DecodeStr(String encodeStr) {
        byte[] b = encodeStr.getBytes();
        Base64 base64 = new Base64();
        b = base64.decode(b);
        return new String(b);
    }

    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * 干净的（整洁的）UUID-不带横杠“-”
     *
     * @return
     */
    public static String getTidyUUID() {
        return getUUID().replace("-", "");
    }

    /**
     * 将参数拼在一起，并计算MD5值,,null值不参与计算
     * 若参数为多个Null值，则将空字符串""加密返回
     *
     * @param objects .
     * @return .
     */
    public static String md5(Object... objects) {
        if (Objs.isEmpty(objects))
            return null;
        final StringBuilder sb = new StringBuilder();
        Loops.forEach(objects, new Consumer<Object>() {
            @Override
            public void accept(Object s) {
                sb.append(s);
            }
        });
        return DigestUtils.md5Hex(sb.toString());
    }

    /**
     * 将参数拼在一起，并计算MD5值,null值也做为"null"参与计算
     *
     * @param objects .
     * @return .
     */
    public static String md5UseNull(Object... objects) {
        if (Objs.isEmpty(objects))
            return null;
        final StringBuilder sb = new StringBuilder();
        Loops.forEach(objects, new Consumer<Object>() {
            @Override
            public void accept(Object s) {
                if (Objs.isEmpty(s)) return;
                sb.append(s);
            }
        });
        return DigestUtils.md5Hex(sb.toString());
    }

    public static String md5(byte[] bs) {
        return DigestUtils.md5Hex(bs);
    }

}
